package com.kcnet.qk.controller.sjgl;

import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.kcnet.qk.constant.Const;
import com.kcnet.qk.controller.BasePeopleController;
import com.kcnet.qk.util.NodeUtil;
import com.kcnet.qk.util.PreferenceHelper;
import com.kcnet.qk.util.RootShell;

import org.greenrobot.eventbus.EventBus;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jesse.nativelogger.NLogger;

/**
 * Created by czc on 2017/7/4.
 * 自动加附近的人
 */

public class AddNearPeopleController extends BasePeopleController {

    private int mType;
    private String mClassName;
    private static String TAG = "添加附近的人";

    private static AddNearPeopleController mRongCloudInstance;

    protected Map<String, Boolean> mRecordMap = Collections.synchronizedMap(new HashMap());

    private String text;
    /**
     * 获取RongCloud 实例。
     *
     * @return RongCloud。
     */
    public static AddNearPeopleController getInstance() {
        if (mRongCloudInstance == null) {
            synchronized (AddNearPeopleController.class) {

                if (mRongCloudInstance == null) {
                    mRongCloudInstance = new AddNearPeopleController();
                }
            }
        }

        return mRongCloudInstance;
    }


    @Override
    public void doTask(AccessibilityEvent event) {
        mType = event.getEventType();
        mClassName = (String) event.getClassName();
        if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.WXUI.LauncherUI)) {
            NLogger.d(TAG,"1.进入微信首页 点击 发现 按钮");
            sleepTime(2000);
            tagTextClick("发现");

            sleepTime(1000);
            NLogger.d(TAG,"2.到达发现界面 点击 新的朋友");
            tagTextClick("附近的人");
        }
        else if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.WXUI.NearbyFriendsUI)
                || mType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED && mClassName.equals(Const.LIST_VIEW)
                || mType == AccessibilityEvent.TYPE_VIEW_SCROLLED && mClassName.equals(Const.LIST_VIEW)) {
            NLogger.d(TAG,"3.到达 附近的人界面列表");
            beginAddPeople();
        } else if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.WXUI.ContactInfoUI)) {
            NLogger.d(TAG,"4.到达 用户资料显示页面 点击打招呼");
            clickSayHiBtn();
        } else if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.WXUI.SayHiEditUI)) {
            NLogger.d(TAG,"5.达到输入好友验证信息 添加好友");
            sleepTimedDefault();
            inputSayHiSentence(text);
            sleepTime(1500);
            NLogger.d(TAG,"6. 添加 发送按钮 添加好友");
            RootShell.click(450,38);
        }
        else if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.WXUI.DialogUI)) {
            tagTextClick("确定");
        }
    }

    private void beginAddPeople() {
        AccessibilityNodeInfo scrllorNodeInfo;
        AccessibilityNodeInfo root = getRoot();
        if (root != null) {
            synchronized (mLock) {
                Log.i("czc", "beginAddPeople");
                scrllorNodeInfo = NodeUtil.findNodeByClassNameAndTime(root, Const.LIST_VIEW, 1);
                if (scrllorNodeInfo == null) {
                    Log.i("czc", "scrllorNode is null ");
                }

                List<AccessibilityNodeInfo> nodeByClassList = NodeUtil.findNodeByClassList(scrllorNodeInfo, Const.LINEAR_LAYOUT);
                for (int i = 0; i < nodeByClassList.size(); i++) {
                    AccessibilityNodeInfo nodeInfo = nodeByClassList.get(i);

                    AccessibilityNodeInfo sexNode = null;
                    List<AccessibilityNodeInfo> imageNodeList = NodeUtil.traverseNodeByClassList(nodeInfo, Const.IMAGE_VIEW);
                    for (AccessibilityNodeInfo imageNode : imageNodeList) {
                        if (imageNode.getContentDescription().equals("男")) {
                            sexNode = imageNode;
                            break;
                        } else if (imageNode.getContentDescription().equals("女")) {
                            sexNode = imageNode;
                            break;
                        }
                    }

                    //找到昵称
                    if (sexNode != null && sexNode.getParent() != null) {
                        AccessibilityNodeInfo userNameNodeInfo = NodeUtil.findNodeByClass(sexNode.getParent(), Const.TEXT_VIEW);
                        if (userNameNodeInfo != null) {
                            if (userNameNodeInfo.getText() != null) {
                                Log.i(TAG, "userName = " + userNameNodeInfo.getText().toString());
                                if (!mRecordMap.containsKey(userNameNodeInfo.getText().toString().trim())) {

                                    AccessibilityNodeInfo clickNode = userNameNodeInfo;
                                    while (clickNode != null && !clickNode.isClickable()) {
                                        clickNode = clickNode.getParent();
                                    }
                                    if (clickNode != null) {

                                        NodeUtil.performClick(clickNode);
                                        break;
                                    }
                                }
                            }

                            if (i == nodeByClassList.size() - 1) {
                                if (scrllorNodeInfo != null) {
                                    NodeUtil.performScroller(scrllorNodeInfo);
                                    break;
                                }
                            }
                        }
                    }

                }
            }
        }
    }

    private void clickSayHiBtn() {
        AccessibilityNodeInfo root = getRoot();
        if (root != null) {
                String name =  findTextNodeInfoId("qj").getText().toString().trim();
                if (mRecordMap.containsKey(name) && mRecordMap.get(name)) {
                    Log.e(TAG, "已经重复添加过,跳过点击返回 "+name);
                    performBackClick();
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {

                    if(currentAddCount  == maxAddCount){
                        currentAddCount = 0;
                        stopTask();
                        Log.e(TAG, "6.任务执行完毕停止任务");
                        //停止任务
                    }
                    currentAddCount++;
                    boolean result = tagTextClick("打招呼");
                    mRecordMap.put(name, result);
                }
            }

    }

    /***
     *
     * @param maxAddCount 添加人数数量
     * @param minAddTime
     * @param maxAddTime
     * @param text 打招呼话语
     */
    public void setParms(int maxAddCount,int minAddTime,int maxAddTime,String text){
        //  stopTask();
        EventBus.getDefault().post(new Boolean(true));
        PreferenceHelper.putBoolean(Const.PREF_KEY_STOP_AUTO_FLAG, true);
        this.maxAddCount = maxAddCount;
        this.minAddTime = minAddTime;
        this.maxAddTime = maxAddTime;
        this.currentAddCount = 0;
        this.text = text;
        mRecordMap.clear();

    }
}
